﻿using ZquenceStudio3Host.Commands.BuiltInCommands;
using ZquenceStudio3Host.DspPlugins.Synthesizers.Nodesizer.Engine.GUI;

namespace ZquenceStudio3Host.DspPlugins.Synthesizers.Nodesizer.Engine.Commands
{
    class ConnectOutputCommand : BaseCommand
    {
        private ModuleOutput mPreviousOutput;
        private ModuleOutput mOutput;
        private ModuleInput mConnectoToInput;
        private BaseModulizerUI mNodisizer;
        private ModulizerUIv2 mNodisizer2;
        private bool mUserNotFirst;

        public override object[] ReferenceObjects
        {
            get { return RefereceObject(mOutput, mConnectoToInput); }
        }

        public ConnectOutputCommand(BaseModulizerUI modisizer, ModuleOutput output, ModuleInput connectToInput)
        {
            mNodisizer = modisizer;
            mOutput = output;
            mConnectoToInput = connectToInput;
            mPreviousOutput = connectToInput.FromOutput;
        }

        public ConnectOutputCommand(ModulizerUIv2 modisizer, ModuleOutput output, ModuleInput connectToInput, ModuleOutput wasConnectedToPeviousOutput)
        {
            mNodisizer2 = modisizer;
            mOutput = output;
            mConnectoToInput = connectToInput;
            mPreviousOutput = wasConnectedToPeviousOutput;
        }

        protected override void Execute()
        {
            // Connect output to input.
            mOutput.AddInput(mConnectoToInput);

            if (mNodisizer2 != null)
            {
                // Flag. First time is always a user that connect. Do not go into parent.
                if (mUserNotFirst)
                {
                    mNodisizer2.GoInto(mOutput.Module.ParentModule);
                }
                mUserNotFirst = true;

                mNodisizer2.UpdateParameterStates();
            }
            else
            {
                // Flag. First time is always a user that connect. Do not go into parent.
                if (mUserNotFirst)
                {
                    mNodisizer.RebuildSynthGraphModuleParent(mOutput.Module);
                }
                mUserNotFirst = true;

                mNodisizer.UpdateParameterStates();
            }
        }

        protected override void UnExecute()
        {
            mOutput.RemoveInput(mConnectoToInput);

            if (mPreviousOutput != null)
            {
                mPreviousOutput.AddInput(mConnectoToInput);
            }

            if (mNodisizer2 != null)
            {
                mNodisizer2.GoInto(mOutput.Module);
                mNodisizer2.UpdateParameterStates();
            }
            else
            {
                mNodisizer.RebuildSynthGraphModuleParent(mOutput.Module);
                mNodisizer.UpdateParameterStates();
            }
        }
        
        public override string ToString()
        {
            return "Connected input.";
        }
    }
}
